package com.dpiinc.TechPage.widget

import android.content.DialogInterface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.core.content.ContextCompat
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.fragment.app.FragmentManager
import com.dpiinc.TechPage.R
import com.dpiinc.TechPage.utils.AppUtil
import kotlinx.android.synthetic.main.comm_dialog_layout.*

/**
 * 通用Dialog
 * Created by X on 2018/7/4.
 */
class CommDialog : BaseDialogFragment(), DialogInterface {

    companion object {
        private const val FRAGMENT_TAG = "CommDialog"
        private val MESSAGE_PADDING_TOP = AppUtil.dp2px(15f).toInt()
        private val MESSAGE_PADDING_BOTTOM = AppUtil.dp2px(20f).toInt()
        private val MESSAGE_PADDING_TOP_NO_TITLE = AppUtil.dp2px(32f).toInt()
        private val MESSAGE_PADDING_BOTTOM_NO_TITLE = AppUtil.dp2px(32f).toInt()
    }

    private var mBuilder: Builder = Builder()
    protected val mDialogButtonClickListener = View.OnClickListener { view ->
        val btnName = view.tag as Int
        when (btnName) {
            DialogInterface.BUTTON_NEGATIVE -> {
                if (mBuilder.mLeftBtnOnClickListener != null) {
                    mBuilder.mLeftBtnOnClickListener!!.onClick(this@CommDialog, DialogInterface.BUTTON_NEGATIVE)
                } else {
                    dismiss()
                }
            }
            DialogInterface.BUTTON_POSITIVE -> {
                if (mBuilder.mRightBtnOnClickListener != null) {
                    mBuilder.mRightBtnOnClickListener!!.onClick(this@CommDialog, DialogInterface.BUTTON_POSITIVE)
                } else {
                    dismiss()
                }
            }
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog?.setCanceledOnTouchOutside(false)
        dialog!!.window?.setBackgroundDrawable(ColorDrawable(0))
        return inflater.inflate(R.layout.comm_dialog_layout, container, false)!!
    }

    override fun onResume() {
        super.onResume()
        setContent()
    }

    fun show(manager: FragmentManager) {
        show(manager, FRAGMENT_TAG)
    }

    private fun setContent() {
        comm_dialog_left_btn.tag = DialogInterface.BUTTON_NEGATIVE
        comm_dialog_right_btn.tag = DialogInterface.BUTTON_POSITIVE
        comm_dialog_left_btn.setOnClickListener(mDialogButtonClickListener)
        comm_dialog_right_btn.setOnClickListener(mDialogButtonClickListener)

        if (!TextUtils.isEmpty(mBuilder.mTitle)) {
            comm_dialog_top_ll.visibility = View.VISIBLE
            comm_dialog_title.text = mBuilder.mTitle
            comm_dialog_message.setTextColor(ContextCompat.getColor(context!!, R.color.comm_dialog_message_color))
            comm_dialog_message.setPadding(comm_dialog_message.paddingLeft, MESSAGE_PADDING_TOP, comm_dialog_message.paddingRight, MESSAGE_PADDING_BOTTOM)
        } else {
            comm_dialog_top_ll.visibility = View.GONE
            comm_dialog_message.setTextColor(ContextCompat.getColor(context!!, R.color.comm_dialog_message_no_title_color))
            comm_dialog_message.setPadding(comm_dialog_message.paddingLeft, MESSAGE_PADDING_TOP_NO_TITLE, comm_dialog_message.paddingRight, MESSAGE_PADDING_BOTTOM_NO_TITLE)
        }

        if (!TextUtils.isEmpty(mBuilder.mMessage)) {
            comm_dialog_center_ll.visibility = View.VISIBLE
            comm_dialog_message.visibility = View.VISIBLE
            comm_dialog_custom_container.visibility = View.GONE
            comm_dialog_message.text = mBuilder.mMessage
        } else if (mBuilder.mCenterCustomerView != null) {
            comm_dialog_center_ll.visibility = View.VISIBLE
            comm_dialog_message.visibility = View.GONE
            comm_dialog_custom_container.visibility = View.VISIBLE
            comm_dialog_custom_container.addView(mBuilder.mCenterCustomerView)
        } else {
            comm_dialog_center_ll.visibility = View.GONE
        }

        if (!TextUtils.isEmpty(mBuilder.mLeftBtnTxt)) {
            comm_dialog_left_btn.visibility = View.VISIBLE
            comm_dialog_left_btn.text = mBuilder.mLeftBtnTxt
            comm_dialog_left_btn.isEnabled = mBuilder.mIsLeftBtnEnable
        } else {
            comm_dialog_left_btn.visibility = View.GONE
            comm_dialog_button_divider.visibility = View.GONE
        }

        if (!TextUtils.isEmpty(mBuilder.mRightBtnTxt)) {
            comm_dialog_right_btn.visibility = View.VISIBLE
            comm_dialog_right_btn.text = mBuilder.mRightBtnTxt
            comm_dialog_right_btn.isEnabled = mBuilder.mIsRightBtnEnable
        } else {
            comm_dialog_right_btn.visibility = View.GONE
            comm_dialog_button_divider.visibility = View.GONE
        }

        //单个按钮的时候，隐藏左侧按钮
        if (mBuilder.mSingleButton) {
            comm_dialog_left_btn.visibility = View.GONE
            comm_dialog_button_divider.visibility = View.GONE
            comm_dialog_right_btn.setBackgroundResource(R.drawable.comm_dialog_single_btn_bg_shape)
        }

        if (mBuilder.mLeftBtnTxtColor != 0) {
            comm_dialog_left_btn.setTextColor(mBuilder.mLeftBtnTxtColor)
        }
        if (mBuilder.mRightBtnTxtColor != 0) {
            comm_dialog_right_btn.setTextColor(mBuilder.mRightBtnTxtColor)
        }

        comm_dialog_bottom_ll.visibility = View.VISIBLE
    }

    override fun cancel() {
        dismiss()
    }

    class Builder {
        var mTitle: String? = null
            private set
        var mMessage: String? = null
            private set
        var mLeftBtnTxt: String? = null
            private set
        var mRightBtnTxt: String? = null
            private set
        var mLeftBtnTxtColor: Int = 0
            private set
        var mRightBtnTxtColor: Int = 0
            private set
        var mLeftBtnOnClickListener: DialogInterface.OnClickListener? = null
            private set
        var mRightBtnOnClickListener: DialogInterface.OnClickListener? = null
            private set
        var mCenterCustomerView: View? = null
            private set
        var mIsLeftBtnEnable: Boolean = true
            private set
        var mIsRightBtnEnable: Boolean = true
            private set
        var mIsCanceledOnTouchOutside: Boolean = false
            private set
        var mSingleButton: Boolean = false
            private set


        fun setTitle(title: String): Builder {
            mTitle = title
            return this
        }

        fun setMessage(message: String): Builder {
            mMessage = message
            return this
        }

        fun setLeftButton(leftBtnTxt: String, leftBtnClickListener: DialogInterface.OnClickListener? = null): Builder {
            mLeftBtnTxt = leftBtnTxt
            mLeftBtnOnClickListener = leftBtnClickListener
            return this
        }

        fun setRightButton(rightBtnTxt: String, rightBtnClickListener: DialogInterface.OnClickListener? = null): Builder {
            mRightBtnTxt = rightBtnTxt
            mRightBtnOnClickListener = rightBtnClickListener
            return this
        }

        fun setLeftButtonTextColor(color: Int): Builder {
            mLeftBtnTxtColor = color
            return this
        }

        fun setRightButtonTextColor(color: Int): Builder {
            mRightBtnTxtColor = color
            return this
        }

        fun setCenterCustomerView(customeView: View): Builder {
            mCenterCustomerView = customeView
            return this
        }

        fun setLeftBtnEnable(enable: Boolean): Builder {
            mIsLeftBtnEnable = enable
            return this
        }

        fun setRightBtnEnable(enable: Boolean): Builder {
            mIsRightBtnEnable = enable
            return this
        }

        fun singleButton(singleButton: Boolean): Builder {
            mSingleButton = singleButton
            return this
        }

        fun setCanceledOnTouchOutside(cancel: Boolean): Builder {
            mIsCanceledOnTouchOutside = cancel
            return this
        }

        fun build(): CommDialog {
            val dialog = CommDialog()
            dialog.mBuilder = this
            return dialog
        }
    }
}
